package com.qiyi.player.core.ad.file
{
   import com.qiyi.player.base.logging.ILogger;
   import flash.utils.ByteArray;
   import com.qiyi.player.base.logging.Log;
   
   public class AdFile extends Object
   {
      
      public function AdFile() {
         this._log = Log.getLogger("com.qiyi.player.core.ad.AdFile");
         super();
         this._flvs = new Vector.<AdFlv>();
      }
      
      private var _flvs:Vector.<AdFlv>;
      
      private var _skiped:Boolean = false;
      
      private var _curReadAdIndex:int;
      
      protected var _log:ILogger;
      
      public function reset() : void {
         var _loc1_:* = 0;
         while(_loc1_ < this._flvs.length)
         {
            this._log.debug("[AdFile.reset]: destroy adFlv index: " + _loc1_ + " id = " + this._flvs[_loc1_].id);
            this._flvs[_loc1_].destroy();
            this._flvs[_loc1_] = null;
            _loc1_++;
         }
         this._flvs.length = 0;
         this._skiped = false;
         this._curReadAdIndex = 0;
      }
      
      public function set skiped(param1:Boolean) : void {
         this._skiped = param1;
      }
      
      public function get skiped() : Boolean {
         return this._skiped;
      }
      
      public function getDuration(param1:int) : int {
         if(param1 >= 0 && param1 < this._flvs.length)
         {
            return this._flvs[param1].duration;
         }
         return 0;
      }
      
      public function getDurationById(param1:String) : int {
         var _loc2_:* = 0;
         while(_loc2_ < this._flvs.length)
         {
            if(this._flvs[_loc2_].id == param1 && !(param1 == null))
            {
               return this._flvs[_loc2_].duration;
            }
            _loc2_++;
         }
         return -1;
      }
      
      public function stopAdFlvById(param1:String) : void {
         var _loc3_:AdFlv = null;
         var _loc2_:* = 0;
         while(_loc2_ < this._flvs.length)
         {
            if(this._flvs[_loc2_].id == param1 && !(param1 == null))
            {
               if(_loc2_ == this._flvs.length - 1)
               {
                  this._log.debug("[AdFile.stopAdFlvById]: reproduce AdFlv index: " + _loc2_ + " id = " + param1);
                  this._flvs.pop().destroy();
                  _loc3_ = new AdFlv();
                  this._flvs.push(_loc3_);
               }
               else if(!this._flvs[_loc2_].exported)
               {
                  this._log.debug("[AdFile.stopAdFlvById]: force to stop AdFlv index: " + _loc2_ + " id = " + param1);
                  this._flvs[_loc2_].exported = true;
                  if(_loc2_ == this._curReadAdIndex)
                  {
                     this._curReadAdIndex++;
                  }
               }
               
               break;
            }
            _loc2_++;
         }
      }
      
      public function get curReadAdIndex() : int {
         return this._curReadAdIndex;
      }
      
      public function get curReadAdId() : String {
         return this._flvs[this._curReadAdIndex].id;
      }
      
      public function set curReadAdIndex(param1:int) : void {
         this._curReadAdIndex = param1;
      }
      
      public function inject(param1:String, param2:ByteArray, param3:Boolean, param4:Boolean) : void {
         var _loc6_:AdFlv = null;
         if(param2 == null || param2.length == 0)
         {
            return;
         }
         var _loc5_:AdFlv = null;
         if(this._flvs.length == 0)
         {
            _loc5_ = new AdFlv();
            this._flvs.push(_loc5_);
         }
         else if(this._flvs.length)
         {
            _loc5_ = this._flvs[this._flvs.length - 1];
         }
         
         _loc5_.inject(param1,param2,param3);
         _loc5_.imported = param4;
         if(_loc5_.imported)
         {
            _loc6_ = new AdFlv();
            this._flvs.push(_loc6_);
         }
      }
      
      public function get empty() : Boolean {
         return this._flvs.length == 0;
      }
      
      public function get exported() : Boolean {
         var _loc1_:* = 0;
         while(_loc1_ < this._flvs.length)
         {
            if((this._flvs[_loc1_].size) && !this._flvs[_loc1_].exported)
            {
               return false;
            }
            _loc1_++;
         }
         return true;
      }
      
      public function get currentAdExported() : Boolean {
         return this._flvs[this._curReadAdIndex].exported;
      }
      
      public function get currentPos() : int {
         return this._flvs[this._curReadAdIndex].currentPos;
      }
      
      public function get header() : ByteArray {
         return this._flvs[this._curReadAdIndex].header;
      }
      
      public function get length() : int {
         return this._flvs.length;
      }
      
      public function get hasLastAd() : Boolean {
         return this._flvs[this._flvs.length - 1].size > 0?this._flvs[this._flvs.length - 1].isLastAd:this._flvs[this._flvs.length - 2].isLastAd;
      }
      
      public function checkIsLastAd(param1:String) : Boolean {
         var _loc2_:* = 0;
         while(_loc2_ < this._flvs.length)
         {
            if(this._flvs[_loc2_].id == param1)
            {
               return this._flvs[_loc2_].isLastAd;
            }
            _loc2_++;
         }
         return false;
      }
      
      public function read(param1:int = -1, param2:int = -1) : ByteArray {
         var _loc3_:ByteArray = null;
         if(this._curReadAdIndex < this._flvs.length && !this._flvs[this._curReadAdIndex].exported)
         {
            _loc3_ = this._flvs[this._curReadAdIndex].read(param1,param2);
         }
         return _loc3_;
      }
   }
}
